package l2s.gameserver.network.serverpackets;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;

import l2s.gameserver.model.ItemInfo;
import l2s.gameserver.model.L2ItemInstance;
import l2s.gameserver.templates.item.L2Item;

public class InventoryUpdate extends L2GameServerPacket
{
	//private List<ItemInfo> _items;
	
	private final Map<Integer, ItemInfo> _items;
	
	public final void addItem(L2ItemInstance item)
	{
		_items.put(item.getObjectId(), new ItemInfo(item));
	}
	
	public final void addNewItem(L2ItemInstance item)
	{
		_items.put(item.getObjectId(), new ItemInfo(item, 1));
	}
	
	public final void addModifiedItem(L2ItemInstance item)
	{
		_items.put(item.getObjectId(), new ItemInfo(item, 2));
	}
	
	public final void addRemovedItem(L2ItemInstance item)
	{
		_items.put(item.getObjectId(), new ItemInfo(item, 3));
	}
	
	public final void addItems(List<L2ItemInstance> items)
	{
		for (L2ItemInstance item : items)
		{
			_items.put(item.getObjectId(), new ItemInfo(item));
		}
	}
	
	public InventoryUpdate()
	{
		_items = new ConcurrentSkipListMap<>();
	}
	
	public InventoryUpdate(ConcurrentSkipListMap<Integer, ItemInfo> items)
	{
		_items = items;
	}
	
	public void addEquipItems(L2ItemInstance[] items)
	{
		if (items != null)
		{
			for (L2ItemInstance item : items)
			{
				if (item != null)
				{
					_items.put(item.getObjectId(), new ItemInfo(item, 3));
				}
			}
		}
	}
	
	@Override
	protected final void writeImpl()
	{
		writeC(0x27);
		writeH(_items.size());
		
		for (ItemInfo temp : _items.values())
		{
			if ((temp == null) || (temp.getItem() == null))
			{
				continue;
			}
			
			L2Item item = temp.getItem();
			
			writeH(temp.getChange());
			writeH(item.getType1());
			writeD(temp.getObjectId());
			writeD(item.getItemId());
			writeD(temp.getCount());
			writeH(item.getType2());
			writeH(temp.getCustomType1());
			writeH(temp.getEquipped());
			writeD(item.getBodyPart());
			writeH(temp.getEnchant());
			writeH(temp.getCustomType2());
			writeD(temp.getAugmentationBoni());
			writeD(temp.getMana());
		}
	}
}